﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_ccEnergyAudit1_Default : BaseTemplatePage
{
    protected int AuditNo = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AcmsContext.EnsureLoggedIn(this);

            if (Request.QueryString["Audit"] != null)
            {
                try
                {
                    AuditNo = int.Parse(Request.QueryString["Audit"]);
                }
                catch (FormatException) { }
            }

            if (AuditNo <= 0 || AuditNo > 2)
            {
                AuditNo = 1;
            }

            if (AcmsContext.CurrentPost != null)
            {
                lnkPupilNumbers.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../");
                lnkWasteAudit.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../WasteAudit");
                lnkAnalysis.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../EnergyAnalysis");
                lnkAuditOne.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("?Audit=1");
                lnkAuditTwo.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("?Audit=2"); 
            }

            DataBind();
        }
    }

    public override void DataBind()
    {
        pnlEnergyEntry.Visible = true;
        pnlEnergyResults.Visible = false;
        pnlMenuEnabled.Visible = true;
        pnlMenuDisabled.Visible = false;
        bool hasPupilNumbersEntered = false;
        ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
        var audits = ClimateChangeChampionsData.CarbonCalculation.GetAllByUserId(context, User.Identity.User.Id);
        if (audits.Count > 0)
        {
            if (audits.Count == 2 && audits.First().KWH != null && audits.Skip(1).First().KWH != null && !string.IsNullOrEmpty(lnkAnalysis.NavigateUrl))
            {
                liAnalysisOff.Visible = false;
                liAnalysisActive.Visible = true;
            }
            else
            {
                liAnalysisOff.Visible = true;
                liAnalysisActive.Visible = false;
            }

            if (audits.First().KWH != null)
            {
                liAuditTwoActive.Visible = true;
                liAuditTwoOff.Visible = false;
            }
            else
            {
                liAuditTwoActive.Visible = false;
                liAuditTwoOff.Visible = true;
            }
             
            ClimateChangeChampionsData.CarbonCalculation calc = audits.Where(x => x.Audit == AuditNo).SingleOrDefault();

            if (audits.Count >= 1 && audits.First().NoPupils > 0)
            {
                hasPupilNumbersEntered = true;
            }

            if (calc != null && calc.KWH != null)
            {
                txtKwH.Text = calc.KWH.ToString();
                lblKGCO2Wk.Text = calc.KGCO2PerWeek.ToString("0.##");
                lblIcKGCO2Wk.Text = calc.PartyBallonsPerWeek.ToString("#,##0");
                
                lblTonCO2Yr.Text = calc.TonnesCO2PerSchoolYear.ToString("0.##");
                lblIcTonCO2Yr.Text = calc.PlanesPerSchoolYear.ToString("#,##0");

                pnlEnergyEntry.Visible = false;
                pnlEnergyResults.Visible = true;
                pnlMenuDisabled.Visible = false;
                pnlMenuEnabled.Visible = true;
            }
        }
        if (!hasPupilNumbersEntered)
        {
            Response.Redirect(AcmsContext.CurrentPost.Parent.Url);
        }
        base.DataBind();
    }

    protected void btnEnter_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
            var audits = ClimateChangeChampionsData.CarbonCalculation.GetAllByUserId(context, User.Identity.User.Id);
            var calc = audits.Where(x => x.Audit == AuditNo).SingleOrDefault();
            if (calc == null && audits.Count >= 1)
            {
                calc = new ClimateChangeChampionsData.CarbonCalculation();
                calc.Audit = AuditNo;
                calc.NoPupils = audits.First().NoPupils;
                calc.UserId = User.Identity.User.Id;
                context.CarbonCalculations.InsertOnSubmit(calc);
            }
            if (calc != null)
            {
                calc.KWH = int.Parse(txtKwH.Text.Trim());
                context.SubmitChanges();
                DataBind();
            }
        }
    }

    protected void btnEditData_Click(object sender, EventArgs e)
    {
        pnlEnergyResults.Visible = false;
        pnlEnergyEntry.Visible = true;
    }

    protected override object SaveViewState()
    {
        ViewState["AuditNo"] = AuditNo;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        AuditNo = (int)ViewState["AuditNo"];

    }

}